<?php
    /**
     * memcache类
     */
    class cache_memcache {        
        private $host; //地址
        private $port; //端口
        
        private static $_memcache;
        
        /**
         * 构造函数
         * 
         * @author zhouqi 2012-10-30 PM 03:41:49
         * @param array $config 配置信息
         * @return cache_memcache
         */
        public function __construct($config) {
            $this->host = $config['host'];
            $this->port = $config['port'];
            
            if(!$this->host || !$this->port) {
                return false;
            }
            
            self::$_memcache = new Memcache();
            self::$_memcache->connect($this->host, $this->port);
        }
        
        /**
         * 获取值
         * 
         * @author zhouqi 2012-10-30 PM 03:43:43
         * @param string $key   KEY值
         */
        public function _get($key) {
            if(!$this->is_connection()) {
                return false;
            }
            
            return self::$_memcache->get($key);
        }
        
        /**
         * 设置值
         * 
         * @author zhouqi 2012-10-30 PM 03:52:12
         * @param string $key   键
         * @param string $val   值
         * @param int $timeout  保存时间
         */
        public function _set($key, $val, $timeout = 60) {
            if(!$this->is_connection()) {
                return false;
            }
            
            return self::$_memcache->set($key, array($val, time(), $timeout), 0, $timeout);
        }
        
        /**
         * 删除键
         * 
         * @author zhouqi 2012-10-30 PM 03:55:34
         * @param string $key   键
         */
        public function _del($key) {
            if(!$this->is_connection()) {
                return false;
            }
            
            return self::$_memcache->delete($key);            
        }
        
        /**
         * 清除所有键
         * 
         * @author zhouqi 2012-10-30 PM 03:56:37
         */
        public function _clean() {
            if(!$this->is_connection()) {
                return false;
            }
            
            return self::$_memcache->flush();
        }
        
        /**
         * 获取解析后的值
         * 
         * @author zhouqi 2012-10-30 PM 03:58:08
         * @param string $key   键
         */
        public function _get_metadata($key) {
            if(!$this->is_connection()) {
                return false;
            }
            
            $data = $this->_get($key);
            
            if(count($data) != 3) {
                return false;
            }
            
            list($data, $time, $endtime) = $data;
            
            return array('expire' => $time + $endtime, 'mtime' => $time, 'data' => $data);
        }
        
        /*+---------------------------private-----------------------------------+*/
        
        /**
         * 是否链接成功
         * 
         * @author zhouqi 2012-10-30 PM 03:44:14
         */
        private function is_connection() {
            if(self::$_memcache == null) {
                return false;
            }
            
            return true;
        }
    }
?>